package com.dxq.oa.interceptor;

import com.alibaba.fastjson.JSON;
import com.dxq.oa.pub.ResponseData;
import com.dxq.oa.utils.JWTUtils;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");

        String token = request.getHeader("token");

        if(token!=null){
            boolean verify = JWTUtils.verify(token);
            if(verify){
                return true;
            }
        }

        ResponseData responseData = new ResponseData("ERROR","token验证失败，请重新登录");
        response.getWriter().write(JSON.toJSONString(responseData));
        return false;
    }
}
